import axios from 'axios'
import { MessageBox, Message,Loading } from 'element-ui'
import router from '@/router'
import store from '@/store'
import { ACCESS_TOKEN } from "@/config/common"
import { config } from "@vue/test-utils";
import { eventBus } from "@/utils/eventBus";

let loading;

window.tipLock = false;

function startLoading() {
  loading = Loading.service({
    lock: true,
    text: "加载中……",
    background: "rgba(0, 0, 0, 0.7)"
  });
}

function endLoading() {
  loading.close();
}

function baseURL() {
  let url = "";
  if (process.env.NODE_ENV === "development") {
    url = process.env.VUE_APP_DEV_API;
  } else if (process.env.NODE_ENV === "production") {
    url = process.env.VUE_APP_PROD_API;
  } else if (process.env.NODE_ENV === "staging") {
    url = process.env.VUE_APP_STAG_API;
  }
  return url;
}

let whiteList = [true]; //员工修改

const service = axios.create({
  baseURL: baseURL(),
  timeout: 80000
});
service.interceptors.request.use(
  config => {
    const token = sessionStorage.getItem(ACCESS_TOKEN);
    if (token) {
      config.headers["ACCESS_TOKEN"] = token;
    }
    if (whiteList.indexOf(config.isTrue) === -1) {
      startLoading();
    }
    return config;
  },
  error => {
    return Promise.reject(error);
  }
);

service.interceptors.response.use(
  response => {
    const res = response.data;
    if (res.code === 200) {
      endLoading();
      return res;
    } else if (res.code === 1) {
      Message({ message: res.msg, type: "error", duration: 3000 });
      endLoading();
      return Promise;
    } else if (res.code === 401) {
      endLoading();
      if (window.tipLock) {
        window.tipLock = false;
        return false;
      }
      MessageBox.confirm(res.msg, "温馨提示", {
        confirmButtonText: "关闭",
        showCancelButton: false,
        showClose: false,
        type: "warning"
      })
        .then(() => {
          sessionStorage.clear();
          store.dispatch("user/resetToken").then(() => {
            router.push({ path: "/login" });
          });
        })
        .catch(() => {});
      window.tipLock = true;
      return Promise;
    } else if (res.code === 600) {
      let msg = res.msg;
      let btnTxt = "关闭";
      if (res.msg.includes("登录已过期")) {
        msg = res.msg.slice(0, 5);
        btnTxt = "去登录";
      }
      if (window.tipLock) {
        window.tipLock = false;
        return false;
      }
      MessageBox.confirm(msg, "温馨提示", {
        confirmButtonText: btnTxt,
        showCancelButton: false,
        showClose: false,
        type: "warning"
      })
        .then(() => {
          sessionStorage.clear();
          router.push({ path: "/login" });
        })
        .catch(() => {});
      endLoading();
      return Promise;
    } else if (res.code === 400) {
      endLoading();
      if (res.msg) {
        Message({ message: res.msg, type: "error", duration: 3000 });
      }
      return Promise;
    } else if (res.code === 4003) {
      router.push({ path: "/login" });
      sessionStorage.clear();
      localStorage.clear();
      endLoading();
      eventBus.$emit("customEvent");
      return Promise;
    } else {
      endLoading();
      if (response.config.isBold) {
        return response;
      } else {
        if (res.msg) {
          Message({ message: res.msg, type: "error", duration: 3000 });
        }
        return res;
      }
    }
  },
  error => {
    endLoading();
    Message({
      message: "系统升级，请联系管理员",
      type: "error",
      duration: 3000
    });
    router.push({ path: "/login" });
    return Promise;
  }
);

export default service
